#include "fedtime.hh"
#include "HwFederateAmbassador.hh"
#include <iostream>

using namespace std;

extern RTI::RTIambassador rtiAll;
extern RTI::ObjectHandle netOffsetAll;

extern RTIfedTime g_currentTime;
extern bool g_bConstrained;
extern bool g_bRegulation;
extern bool g_granted;

HwFederateAmbassador::HwFederateAmbassador() {
}

HwFederateAmbassador::~HwFederateAmbassador() throw(RTI::FederateInternalError) {
}

////////////////////////////////////
// Federation Management Services //
////////////////////////////////////

void HwFederateAmbassador::synchronizationPointRegistrationSucceeded(
        const char *label) // supplied C4)
throw(
RTI::FederateInternalError) {
}

void HwFederateAmbassador::synchronizationPointRegistrationFailed(
        const char *label) // supplied C4)
throw(
RTI::FederateInternalError) {
}

void HwFederateAmbassador::announceSynchronizationPoint(
        const char *label, // supplied C4
        const char *tag)   // supplied C4
throw(
RTI::FederateInternalError) {
}

void HwFederateAmbassador::federationSynchronized(
        const char *label) // supplied C4)
throw(
RTI::FederateInternalError) {
}

void HwFederateAmbassador::initiateFederateSave(
        const char *label) // supplied C4
throw(
RTI::UnableToPerformSave,
RTI::FederateInternalError) {
}

void HwFederateAmbassador::federationSaved() throw(
RTI::FederateInternalError) {
}

void HwFederateAmbassador::federationNotSaved() throw(
RTI::FederateInternalError) {
}

void HwFederateAmbassador::requestFederationRestoreSucceeded(
        const char *label) // supplied C4
throw(
RTI::FederateInternalError) {
}

void HwFederateAmbassador::requestFederationRestoreFailed(
        const char *label) // supplied C4
throw(
RTI::FederateInternalError) {
}

void HwFederateAmbassador::requestFederationRestoreFailed(
        const char *label,
        const char *reason) // supplied C4
throw(
RTI::FederateInternalError) {
}

void HwFederateAmbassador::federationRestoreBegun() throw(
RTI::FederateInternalError) {
}

void HwFederateAmbassador::initiateFederateRestore(
        const char *label,          // supplied C4
        RTI::FederateHandle handle) // supplied C1
throw(
RTI::SpecifiedSaveLabelDoesNotExist,
RTI::CouldNotRestore,
RTI::FederateInternalError) {
}

void HwFederateAmbassador::federationRestored() throw(
RTI::FederateInternalError) {
}

void HwFederateAmbassador::federationNotRestored() throw(
RTI::FederateInternalError) {
}

/////////////////////////////////////
// Declaration Management Services //
/////////////////////////////////////

void HwFederateAmbassador::startRegistrationForObjectClass(
        RTI::ObjectClassHandle theClass) // supplied C1
throw(
RTI::ObjectClassNotPublished,
RTI::FederateInternalError) {
}

void HwFederateAmbassador::stopRegistrationForObjectClass(
        RTI::ObjectClassHandle theClass) // supplied C1
throw(
RTI::ObjectClassNotPublished,
RTI::FederateInternalError) {
}

void HwFederateAmbassador::turnInteractionsOn(
        RTI::InteractionClassHandle theHandle) // supplied C1
throw(
RTI::InteractionClassNotPublished,
RTI::FederateInternalError) {
}

void HwFederateAmbassador::turnInteractionsOff(
        RTI::InteractionClassHandle theHandle) // supplied C1
throw(
RTI::InteractionClassNotPublished,
RTI::FederateInternalError) {
}

////////////////////////////////
// Object Management Services //
////////////////////////////////

void HwFederateAmbassador::discoverObjectInstance(
        RTI::ObjectHandle theObject,           // supplied C1
        RTI::ObjectClassHandle theObjectClass, // supplied C1
        const char *theObjectName)             // supplied C4
throw(
RTI::CouldNotDiscover,
RTI::ObjectClassNotKnown,
RTI::FederateInternalError) {
}

void HwFederateAmbassador::reflectAttributeValues(
        RTI::ObjectHandle theObject,                           // supplied C1
        const RTI::AttributeHandleValuePairSet &theAttributes, // supplied C4
        const RTI::FedTime &theTime,                           // supplied C1
        const char *theTag,                                    // supplied C4
        RTI::EventRetractionHandle theHandle)                  // supplied C1
throw(
RTI::ObjectNotKnown,
RTI::AttributeNotKnown,
RTI::FederateOwnsAttributes,
RTI::InvalidFederationTime,
RTI::FederateInternalError) {
}

void HwFederateAmbassador::reflectAttributeValues(
        RTI::ObjectHandle theObject,                           // supplied C1
        const RTI::AttributeHandleValuePairSet &theAttributes, // supplied C4
        const char *theTag)                                    // supplied C4
throw(
RTI::ObjectNotKnown,
RTI::AttributeNotKnown,
RTI::FederateOwnsAttributes,
RTI::FederateInternalError) {
}

void HwFederateAmbassador::receiveInteraction(
        RTI::InteractionClassHandle theInteraction,            // supplied C1
        const RTI::ParameterHandleValuePairSet &theParameters, // supplied C4
        const RTI::FedTime &theTime,                           // supplied C4
        const char *theTag,                                    // supplied C4
        RTI::EventRetractionHandle theHandle)                  // supplied C1
throw(
RTI::InteractionClassNotKnown,
RTI::InteractionParameterNotKnown,
RTI::InvalidFederationTime,
RTI::FederateInternalError) {
    // call the next service.
    this->receiveInteraction(theInteraction, theParameters, theTag);
}

void HwFederateAmbassador::receiveInteraction(
        RTI::InteractionClassHandle theInteraction,            // supplied C1
        const RTI::ParameterHandleValuePairSet &theParameters, // supplied C4
        const char *theTag)                                    // supplied C4
throw(
RTI::InteractionClassNotKnown,
RTI::InteractionParameterNotKnown,
RTI::FederateInternalError) {
    //cout<<"......"<<endl;
    RTI::ParameterHandle paraHandle;
    RTI::ULong valueLength;
    string value;

    if (strcmp(theTag, "sync_time") == 0) {
        string t_sync;
        string t_InfoNum;

        paraHandle = theParameters.getHandle(0);
        t_sync.resize(theParameters.getValueLength(0));
        theParameters.getValue(0, (char *) t_sync.c_str(), valueLength);

        paraHandle = theParameters.getHandle(1);
        t_InfoNum.resize(theParameters.getValueLength(1));
        theParameters.getValue(1, (char *) t_InfoNum.c_str(), valueLength);

    }
}

void HwFederateAmbassador::removeObjectInstance(
        RTI::ObjectHandle theObject,          // supplied C1
        const RTI::FedTime &theTime,          // supplied C4
        const char *theTag,                   // supplied C4
        RTI::EventRetractionHandle theHandle) // supplied C1
throw(
RTI::ObjectNotKnown,
RTI::InvalidFederationTime,
RTI::FederateInternalError) {
}

void HwFederateAmbassador::removeObjectInstance(
        RTI::ObjectHandle theObject, // supplied C1
        const char *theTag)          // supplied C4
throw(
RTI::ObjectNotKnown,
RTI::FederateInternalError) {
}

void HwFederateAmbassador::attributesInScope(
        RTI::ObjectHandle theObject,                  // supplied C1
        const RTI::AttributeHandleSet &theAttributes) // supplied C4
throw(
RTI::ObjectNotKnown,
RTI::AttributeNotKnown,
RTI::FederateInternalError) {
}

void HwFederateAmbassador::attributesOutOfScope(
        RTI::ObjectHandle theObject,                  // supplied C1
        const RTI::AttributeHandleSet &theAttributes) // supplied C4
throw(
RTI::ObjectNotKnown,
RTI::AttributeNotKnown,
RTI::FederateInternalError) {
}

void HwFederateAmbassador::provideAttributeValueUpdate(
        RTI::ObjectHandle theObject,                  // supplied C1
        const RTI::AttributeHandleSet &theAttributes) // supplied C4
throw(
RTI::ObjectNotKnown,
RTI::AttributeNotKnown,
RTI::AttributeNotOwned,
RTI::FederateInternalError) {
    LOG(ERROR)<<"请求更新";
    RTI::AttributeHandleValuePairSet* pAttrs = NULL;
    pAttrs = RTI::AttributeSetFactory::create (2);
    // Town01 vehicle:51 
    string xStr = "0.06";
    string yStr = "328.61";
    // Town04
//    string xStr = "503.02";
//    string yStr = "423.76";
    pAttrs->add(netOffsetAll, xStr.c_str(), xStr.size());
    pAttrs->add(netOffsetAll, yStr.c_str(), yStr.size());
    rtiAll.updateAttributeValues(netOffsetAll, *pAttrs, "netOffset");
}

void HwFederateAmbassador::turnUpdatesOnForObjectInstance(
        RTI::ObjectHandle theObject,                  // supplied C1
        const RTI::AttributeHandleSet &theAttributes) // supplied C4
throw(
RTI::ObjectNotKnown,
RTI::AttributeNotOwned,
RTI::FederateInternalError) {
}

void HwFederateAmbassador::turnUpdatesOffForObjectInstance(
        RTI::ObjectHandle theObject,                  // supplied C1
        const RTI::AttributeHandleSet &theAttributes) // supplied C4
throw(
RTI::ObjectNotKnown,
RTI::AttributeNotOwned,
RTI::FederateInternalError) {
}

///////////////////////////////////
// Ownership Management Services //
///////////////////////////////////

void HwFederateAmbassador::requestAttributeOwnershipAssumption(
        RTI::ObjectHandle theObject,                      // supplied C1
        const RTI::AttributeHandleSet &offeredAttributes, // supplied C4
        const char *theTag)                               // supplied C4
throw(
RTI::ObjectNotKnown,
RTI::AttributeNotKnown,
RTI::AttributeAlreadyOwned,
RTI::AttributeNotPublished,
RTI::FederateInternalError) {
}

void HwFederateAmbassador::attributeOwnershipDivestitureNotification(
        RTI::ObjectHandle theObject,                       // supplied C1
        const RTI::AttributeHandleSet &releasedAttributes) // supplied C4
throw(
RTI::ObjectNotKnown,
RTI::AttributeNotKnown,
RTI::AttributeNotOwned,
RTI::AttributeDivestitureWasNotRequested,
RTI::FederateInternalError) {
}

void HwFederateAmbassador::attributeOwnershipAcquisitionNotification(
        RTI::ObjectHandle theObject,                      // supplied C1
        const RTI::AttributeHandleSet &securedAttributes) // supplied C4
throw(
RTI::ObjectNotKnown,
RTI::AttributeNotKnown,
RTI::AttributeAcquisitionWasNotRequested,
RTI::AttributeAlreadyOwned,
RTI::AttributeNotPublished,
RTI::FederateInternalError) {
}

void HwFederateAmbassador::attributeOwnershipUnavailable(
        RTI::ObjectHandle theObject,                  // supplied C1
        const RTI::AttributeHandleSet &theAttributes) // supplied C4
throw(
RTI::ObjectNotKnown,
RTI::AttributeNotKnown,
RTI::AttributeAlreadyOwned,
RTI::AttributeAcquisitionWasNotRequested,
RTI::FederateInternalError) {
}

void HwFederateAmbassador::requestAttributeOwnershipRelease(
        RTI::ObjectHandle theObject,                        // supplied C1
        const RTI::AttributeHandleSet &candidateAttributes, // supplied C4
        const char *theTag)                                 // supplied C4
throw(
RTI::ObjectNotKnown,
RTI::AttributeNotKnown,
RTI::AttributeNotOwned,
RTI::FederateInternalError) {
}

void HwFederateAmbassador::confirmAttributeOwnershipAcquisitionCancellation(
        RTI::ObjectHandle theObject,                  // supplied C1
        const RTI::AttributeHandleSet &theAttributes) // supplied C4
throw(
RTI::ObjectNotKnown,
RTI::AttributeNotKnown,
RTI::AttributeAlreadyOwned,
RTI::AttributeAcquisitionWasNotCanceled,
RTI::FederateInternalError) {
}

void HwFederateAmbassador::informAttributeOwnership(
        RTI::ObjectHandle theObject,       // supplied C1
        RTI::AttributeHandle theAttribute, // supplied C1
        RTI::FederateHandle theOwner)      // supplied C1
throw(
RTI::ObjectNotKnown,
RTI::AttributeNotKnown,
RTI::FederateInternalError) {
}

void HwFederateAmbassador::attributeIsNotOwned(
        RTI::ObjectHandle theObject,       // supplied C1
        RTI::AttributeHandle theAttribute) // supplied C1
throw(
RTI::ObjectNotKnown,
RTI::AttributeNotKnown,
RTI::FederateInternalError) {
}

void HwFederateAmbassador::attributeOwnedByRTI(
        RTI::ObjectHandle theObject,       // supplied C1
        RTI::AttributeHandle theAttribute) // supplied C1
throw(
RTI::ObjectNotKnown,
RTI::AttributeNotKnown,
RTI::FederateInternalError) {
}

//////////////////////////////
// Time Management Services //
//////////////////////////////

// 启用时间限制。RTI同意将仿真成员设置为时间管理受限的成员
void HwFederateAmbassador::timeConstrainedEnabled(
        const RTI::FedTime &theFederateTime) // supplied C4
throw(
RTI::InvalidFederationTime,
RTI::EnableTimeConstrainedWasNotPending,
RTI::FederateInternalError) {
    g_currentTime = theFederateTime;
    g_bConstrained = true;
    LOG(TRACE) << "RTI同意将仿真成员设置为时间管理受限的成员";
}

void HwFederateAmbassador::timeRegulationEnabled(
        const RTI::FedTime &theFederateTime) // supplied C4
throw(
RTI::InvalidFederationTime,
RTI::EnableTimeRegulationWasNotPending,
RTI::FederateInternalError) {
    g_currentTime = theFederateTime;
    g_bRegulation = true;
    LOG(TRACE) << "RTI同意将仿真成员设置为时间管控成员";
}

// 时间提前授予。RTI同意仿真成员推进到下一步。
void HwFederateAmbassador::timeAdvanceGrant(
        const RTI::FedTime &theTime) // supplied C4
throw(
RTI::InvalidFederationTime,
RTI::TimeAdvanceWasNotInProgress,
RTI::FederateInternalError) {
    g_currentTime = theTime;
    g_granted = true;
    LOG(TRACE) << "RTI同意仿真成员推进到: " << theTime;
}

void HwFederateAmbassador::requestRetraction(
        RTI::EventRetractionHandle theHandle) // supplied C1
throw(
RTI::EventNotKnown,
RTI::FederateInternalError) {
}
